/**
 * GUI.java 
 * @author Matan Ginzburg 203827753
 *
 */

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.HashSet;

import javax.swing.*;


public class GUI extends JFrame implements MouseListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField N, tau, delta, norm, file, answer;
	private JButton click;
	
	public GUI() {
		super("SFT");
		JPanel temp;
		
		this.setLayout(new GridLayout(5, 1));
		this.setSize(1000, 200);
		
		N = new JTextField();
		tau = new JTextField();
		delta = new JTextField();
		norm = new JTextField();
		file = new JTextField();
		answer = new JTextField();
		click = new JButton("Start!");
		
		/**N and delta**/
		temp = new JPanel();
		temp.setLayout(new GridLayout(1,4));
		temp.add(new JLabel("N:"));
		temp.add(N);
		temp.add(new JLabel("tau:"));
		temp.add(tau);
		this.add(temp);
		
		/**delta and norm**/
		temp = new JPanel();
		temp.setLayout(new GridLayout(1,4));
		temp.add(new JLabel("delta:"));
		temp.add(delta);
		temp.add(new JLabel("norm:"));
		temp.add(norm);
		this.add(temp);
		
		/**XML**/
		temp = new JPanel();
		temp.setLayout(new GridLayout(1,2));
		temp.add(new JLabel("XML:"));
		temp.add(file);
		this.add(temp);
		
		/**Answer**/
		temp = new JPanel();
		temp.setLayout(new GridLayout(1,2));
		temp.add(new JLabel("Answer"));
		answer.setEditable(false);
		temp.add(answer);
		this.add(temp);
		
		/**Button**/
		temp = new JPanel();
		click.addMouseListener(this);
		temp.add(click);
		this.add(temp);
		
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
		
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (!(e.getComponent() instanceof JButton))
			return;
		JButton b = (JButton) e.getComponent();
		if(b!=click)
			return;
		Function f = null;
		int n = Integer.parseInt(N.getText());
		double t = Double.parseDouble(tau.getText());
		double d = Double.parseDouble(delta.getText());
		double no = Double.parseDouble(norm.getText());
		String fi = file.getText();
		HashSet<Integer> res;
		if(fi.equals(""))
			f = new Function(n);
		else {
			try {
				f = XMLParse.getFunction(n,new File(fi));
			} catch (Exception e1) {
				answer.setText("An error occured when opening file.");
				return;
			}
		}
		
		
		res = Algorithm.SFT(f, t, d, no);
		if(res.size()>0)
			answer.setText("Heavy coefficients are: " + printList(res));
		else
			answer.setText("No heavy coefficients were found.");
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	public static String printList(HashSet<Integer> list){
		
		String s = "";
		
		for (int x : list) {
			if(s=="")
				s = s + x;
			else
				s = s + ", " + x;
		}
		return s;
	}
	
	public static Complex getValue(int i) {
		String s = (String) JOptionPane.showInputDialog(new JFrame(),
							"Please enter the value of the function at x=" + i + " in the form of a+bi, do no click Cancel!",
							"Value at " + i,
							JOptionPane.PLAIN_MESSAGE);
		return Complex.parseComplex(s);
	}
	
	public static void main(String[] args) {		
		//System.out.println(getValue(100));
		new GUI();
	}

}
